package com.xlw.ui.view;

import android.opengl.GLSurfaceView;
import android.opengl.GLU;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/**
 * Created by xinliwei on 2015/7/5.
 */
public class MyGLRenderer implements GLSurfaceView.Renderer {
    private Cube m_cube;
    private float ang = 0.0f;

    public MyGLRenderer(){

        m_cube = new Cube();

    }
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //设置清除色
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);//设置视口
        float ratio = (float) width / height;
        gl.glMatrixMode(GL10.GL_PROJECTION);   // 设置当前矩阵为投影矩阵
        gl.glLoadIdentity();                 // 重置矩阵为初始值
        gl.glFrustumf(-ratio, ratio, -1, 1, 3, 100);  // 根据长宽比设置投影矩阵


    }

    @Override
    public void onDrawFrame(GL10 gl) {
        // 清空缓存
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        // 设置当前矩阵为模型视图模式 //
        gl.glMatrixMode(GL10.GL_MODELVIEW);

        gl.glLoadIdentity();   // 将matrix重置为其默认状态

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

        // 设置视点
        GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
        gl.glRotatef(ang, 0f, 0.2f, 0f);

        m_cube.draw(gl);

        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

        ang += 1.0f;

    }
}
